﻿@model ByHand.Model.Product
@{
    ViewBag.Title = "Option";
    Layout = "~/Views/Shared/_Layout.cshtml";
    var ops = ViewData["productOption"] as List<ByHand.Model.ProductOption>;
    int i = 0;
    if (ops != null)
    {
        i = ops.Count;
    }
}

<h2>Thêm Option cho Product: @Model.Title</h2>
@if (TempData["mess"] != null)
{
    <div class="alert alert-success">@TempData["mess"]</div>
}
@using (Html.BeginForm("SaveOption", "Product", FormMethod.Post, new { Class = "form-horizontal" }))
{
    <input type="hidden" name="pID" value="@Model.ID" />
    if (ops != null)
    {
        for (int j = 0; j < ops.Count; j++)
        {
            var ovs = ops[j].OptionValue.Split(';').Where(item => item.Length > 0).ToArray();
    <div class="form-group">
        <label for="inputEmail1" class="col-lg-2 control-label">Tên Option</label>
        <div class="col-lg-4">
            <input type="text" class="form-control" name="optionName" placeholder="Tên Option" value="@ops[j].OptionName" />
            <input type="hidden" name="countValue" id="hid-count-@(j)" value="@ovs.Length" />
        </div>
        <label for="inputEmail1" class="col-lg-2 control-label">Giá trị Option</label>
        <div class="col-lg-4">
            @foreach (var item in ovs)
            {
                <input type="text" class="form-control" style="margin-top: 3px;" value="@item" name="optionValue" placeholder="Value Option">
            }
            <a href="javascript:;" onclick="addOV(this,@j)">Click add more ...</a>
        </div>
    </div>
        }

    }
    <div class="form-group">
        <label for="inputEmail1" class="col-lg-2 control-label">Tên Option</label>
        <div class="col-lg-4">
            <input type="text" class="form-control" name="optionName" id="inputEmail1" placeholder="Tên Option">
            <input type="hidden" name="countValue" value="1" id="hid-count-@(i)" />
        </div>
        <label for="inputEmail1" class="col-lg-2 control-label">Giá trị Option</label>
        <div class="col-lg-4" id="div-ov">
            <input type="text" class="form-control" style="margin-top: 3px;" name="optionValue" placeholder="Value Option">
            <a href="javascript:;" onclick="addOV(this,@i)">Click add more value </a>
        </div>
    </div>
    <a href="javascript:;" onclick="addOP(this)">Click add more Option </a> <br />
    <button type="submit" class="btn btn-primary">Lưu</button>
}

<script type="text/javascript">
    var numberON = parseInt('@(i + 1)');
    function addOV(o, count) {
        $('<input type="text" class="form-control" style="margin-top: 3px;" name="optionValue" placeholder="Value Option" />').insertBefore(o);
        var c = parseInt($('#hid-count-' + count).val())
        $('#hid-count-' + count).val(c + 1);
    }
    function addOP(o) {
        $('<div class="form-group">'
        + '<label for="inputEmail1" class="col-lg-2 control-label">Tên Option</label>'
        + '<div class="col-lg-4">'
        + '<input type="text" class="form-control" name="optionName" id="inputEmail1" placeholder="Tên Option">'
        + '<input type="hidden" name="countValue" value="1" id="hid-count-' + numberON + '" />'
        + '</div>'
        + '<label for="inputEmail1" class="col-lg-2 control-label">Giá trị Option</label>'
        + '<div class="col-lg-4" id="div-ov">'
        + '<input type="text" class="form-control" style="margin-top: 3px;" name="optionValue" placeholder="Value Option">'
        + '<a href="javascript:;" onclick="addOV(this,' + numberON + ')">Click add more value </a>'
        + '</div>'
        + '</div>').insertBefore(o);
        numberON++;
    }
</script>
